package com.pinyougou.content.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.PageHelper;
import com.pinyougou.mapper.TbContentMapper;
import com.pinyougou.pojo.TbContent;
import com.pinyougou.pojo.TbContentExample;
import com.pinyougou.pojo.TbContentExample.Criteria;
import com.pinyougou.content.service.ContentService;
import org.springframework.data.redis.core.RedisTemplate;


/**
 * 服务实现层
 * @author Administrator
 *
 */
@Service
public class ContentServiceImpl implements ContentService {

	@Autowired
	private TbContentMapper contentMapper;

	@Autowired
	private RedisTemplate redisTemplate;

	/**
	 * 查询全部
	 */
	@Override
	public List<TbContent> findAll() {
		return contentMapper.selectByExample(null);
	}

	/**
	 * 按分页查询
	 */
//	@Override
//	public PageInfo<TbContent> findPage(int pageNum, int pageSize) {
//		PageHelper.startPage(pageNum, pageSize);
//		List<TbContent> list = contentMapper.selectByExample(null);
//		PageInfo<TbContent> info = new PageInfo<TbContent>(list);
//		return info;
//	}

	/**
	 * 增加
	 */
	@Override
	public void add(TbContent content) {
		contentMapper.insert(content);

		redisTemplate.boundHashOps("content").delete(content.getCategoryId()); //清除redis缓存
	}

	
	/**
	 * 修改
	 */
	@Override
	public void update(TbContent content){
		//1.清除修改前的redis缓存
		//1.1查询修改前的分类id(因为修改可能会改categoryId,但主键id不会改,所以要根据主键id查询广告再查询categoryId)
		Long categoryId = contentMapper.selectByPrimaryKey(content.getId()).getCategoryId();
		//1.2根据分类categoryId清除修改前的redis缓存
		redisTemplate.boundHashOps("content").delete(categoryId); //根据分类categoryId清除redis缓存
		contentMapper.updateByPrimaryKey(content);

		//2.清除修改后的redis缓存
		//如果分类categoryId也修改了,则也清除修改后的redis缓存
		if (categoryId.longValue() != content.getCategoryId().longValue()){
			redisTemplate.boundHashOps("content").delete(content.getCategoryId()); //根据分类categoryId清除redis缓存
		}
	}	
	
	/**
	 * 根据ID获取实体
	 * @param id
	 * @return
	 */
	@Override
	public TbContent findOne(Long id){
		return contentMapper.selectByPrimaryKey(id);
	}

	/**
	 * 批量删除
	 */
	@Override
	public void delete(Long[] ids) {
		for(Long id:ids){
			Long categoryId = contentMapper.selectByPrimaryKey(id).getCategoryId();//广告分类categoryId
			redisTemplate.boundHashOps("content").delete(categoryId);//清除redis缓存
			contentMapper.deleteByPrimaryKey(id);
		}		
	}
	
	
	@Override
	public PageInfo<TbContent> findPage(TbContent content, int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		
		TbContentExample example=new TbContentExample();
		Criteria criteria = example.createCriteria();
		
		if(content!=null){			
						if(content.getTitle()!=null && content.getTitle().length()>0){
				criteria.andTitleLike("%"+content.getTitle()+"%");
			}
			if(content.getUrl()!=null && content.getUrl().length()>0){
				criteria.andUrlLike("%"+content.getUrl()+"%");
			}
			if(content.getPic()!=null && content.getPic().length()>0){
				criteria.andPicLike("%"+content.getPic()+"%");
			}
			if(content.getStatus()!=null && content.getStatus().length()>0){
				criteria.andStatusLike("%"+content.getStatus()+"%");
			}
	
		}

		List<TbContent> list = contentMapper.selectByExample(example);
		PageInfo<TbContent> info = new PageInfo<TbContent>(list);
		return info;
	}



	/**
	 * 根据广告类目id查询广告列表
	 * @param categoryId
	 * @return
	 */
	@Override
	public List<TbContent> findByCategoryId(Long categoryId) {

		//1.先查询redis缓存数据库
		List<TbContent> ContentList= (List<TbContent>) redisTemplate.boundHashOps("content").get(categoryId);

		if (ContentList == null){ //如果redis缓存中没有,则查询数据库并存入redis缓存
			TbContentExample example = new TbContentExample();
			Criteria criteria = example.createCriteria();

			criteria.andCategoryIdEqualTo(categoryId);//指定类目categoryId
			criteria.andStatusEqualTo("1");//指定状态status
			example.setOrderByClause("sort_order");//根据sort_order字段排序

			List<TbContent> contentList = contentMapper.selectByExample(example);

			redisTemplate.boundHashOps("content").put(categoryId,contentList); //存入缓存
			System.out.println("数据库数据存入缓存");
		}
		else {
			System.out.println("从缓存中读取数据");
		}
		return ContentList;
	}

}
